package com.jxtc.enterprise.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.admin.service.AdminCompanyManagementService;
import com.jxtc.enterprise.common.aspectj.TokenRequired;
import com.jxtc.enterprise.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * 管理员视图，菜单管理，配置菜单
 */
@RestController
@RequestMapping("/jxtc/enterprise/admin/menuManage")
@RequiredArgsConstructor
@Slf4j
@TokenRequired(role = TokenRequired.ROLE_ADMIN)
public class AdminMenuManagementController {

    private final AdminCompanyManagementService adminViewCompanyManagementService;

    public static ThreadLocal<String> TL_REQUEST_ID = new ThreadLocal<>();


    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 200 表示新增公司配置信息成功，data 字段为返回的公司信息")
    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 400 表示新增公司配置信息失败，message 字段则会提供错误信息")
    @Operation(summary = "1.3.1 管理员视图，查看某个公司的所有菜单", tags = "1.2 管理员公司管理", operationId = "1.2.2")
    @GetMapping("/queryMenusByCompanyIdFromAdminView")
    public Result<?> queryMenusByCompanyIdFromAdminView(
            @RequestParam("companyName") String companyName,
            @RequestParam("tenantName") String tenantName,
            @RequestParam("state") int state,
            @RequestParam("pageNo") int pageNo,
            @RequestParam("pageSize") int pageSize) {

        String requestId = UUID.randomUUID().toString();
        log.info("requestId {}, admin view, /queryMenusByCompanyIdFromAdminView, companyName: {}, tenantName: {}, state: {}", requestId, companyName, tenantName, state);
        TL_REQUEST_ID.set(requestId);

        Result<Object> resultData = adminViewCompanyManagementService.queryMenusOfCompany(tenantName, companyName, state, pageNo, pageSize);
        log.info("/queryAllCompanyBriefInfosByPage, requestId: {}, response: {}", requestId, resultData);
        return resultData;
    }

    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 200 表示新增公司配置信息成功，data 字段为返回的公司信息")
    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 400 表示新增公司配置信息失败，message 字段则会提供错误信息")
    @Operation(summary = "1.3.2 管理员视图，查看某个菜单中某一餐的详情", tags = "1.3 配置菜单 页面", operationId = "1.3.2")
    @GetMapping("/queryMenuDetailByMealTypeFromAdminView")
    public Result<?> queryMenuDetailByMealTypeFromAdminView(
            @RequestParam("companyId") String companyId,
            @RequestParam("menuId") String menuId,
            @RequestParam("mealType") String mealType) {

        String requestId = UUID.randomUUID().toString();
        log.info("requestId {}, admin view, /queryMenuDetailByMealTypeFromAdminView, companyId: {}, menuId: {}, mealType: {}", requestId, companyId, menuId, mealType);
        TL_REQUEST_ID.set(requestId);

        Result<Object> result = adminViewCompanyManagementService.queryMenuDetailByMealType(companyId, menuId, mealType);
        log.info("requestId: {}, admin view, /adminView/queryMenuDetailForCompany, response successfully", requestId);
        return result;
    }


    @Operation(summary = "1.3.3 管理员视图，查询店铺针对公司某一餐的配置", tags = {"1.3 管理员公司管理"}, operationId = "1.3.3")
    @GetMapping("/queryStoreConfigForCompanyAdminView")
    public Result<?> queryStoreConfigForCompanyAdminView(@RequestParam("storeConfigId") String storeConfigId,
                                                @RequestParam("companyId") String companyId,
                                                @RequestParam("storeId") String storeId,
                                                @RequestParam("mealType") String mealType) {
        String requestId = UUID.randomUUID().toString();
        log.info("requestId {}, admin view, /adminView/queryStoreConfigForCompanyAdminView, storeConfigId: {}, companyId: {}, storeId: {}, mealType: {}",
                requestId, storeConfigId, companyId, storeId, mealType);
        Result<JSONObject> result = adminViewCompanyManagementService.queryStoreConfigForCompany(requestId, storeConfigId, mealType);
        log.info("requestId {}, admin view, /adminView/queryStoreConfigForCompanyAdminView, response successfully", requestId);
        return result;
    }

}
